package com.goose.bmm.controler.fragment;

import java.util.Calendar;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;

import com.goose.bmm.R;
import com.goose.bmm.model.business.Constants;
import com.goose.bmm.model.business.DataGlobal;

public class SettingMatchFragment extends Fragment implements OnClickListener {

	private static final int NBA_STANDAR_OPTION = 1;
	private static final int NCAA_STANDAR_OPTION = 2;
	private static final int CUSTOM_OPTION = 3;

	private final String[] NB_PERIOD_NBA = { Constants.NB_PERIOD_NBA + "" };
	private final String[] NB_PERIOD_NCAA = { Constants.NB_PERIOD_NCAA + "" };
	private final String[] NB_PERIOD_CT = { Constants.NB_PERIOD_NCAA + "",
			Constants.NB_PERIOD_NBA + "" };

	private final String[] TIME_PERIOD_NBA = { Constants.TIME_PERIOD_NBA + "" };
	private final String[] TIME_PERIOD_NCAA = { Constants.TIME_PERIOD_NCAA + "" };
	private final String[] TIME_PERIOD_CT = { Constants.TIME_PERIOD_CT_1 + "",
			Constants.TIME_PERIOD_CT_2 + "", Constants.TIME_PERIOD_CT_3 + "",
			Constants.TIME_PERIOD_CT_4 + "", Constants.TIME_PERIOD_CT_5 + "" };

	private final String[] SHOT_TIME_NBA = { Constants.SHOT_TIME_NBA + "" };
	private final String[] SHOT_TIME_NCAA = { Constants.SHOT_TIME_NCAA + "" };
	private final String[] SHOT_TIME_CT = { Constants.SHOT_TIME_NBA + "",
			Constants.SHOT_TIME_NCAA + "" };

	private View mView;
	private RadioButton mRadioButtonNBA;
	private RadioButton mRadioButtonNCAA;
	private RadioButton mRadioButtonCustom;
	private EditText mEditTextTournament;
	private EditText mEditTextReferee1;
	private EditText mEditTextReferee2;
	private EditText mEditTextGymnasium;
	private Spinner mSpinnerNumberOfPeriod;
	private Spinner mSpinnerTimePeriod;
	private Spinner mSpinnerShotTime;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_setting_match, container,
				false);

		mRadioButtonNBA = (RadioButton) mView
				.findViewById(R.id.radBtn_nba_standar);
		mRadioButtonNCAA = (RadioButton) mView
				.findViewById(R.id.radBtn_ncaa_standar);
		mRadioButtonCustom = (RadioButton) mView
				.findViewById(R.id.radBtn_custom_options);
		mEditTextTournament = (EditText) mView
				.findViewById(R.id.edt_tournament);
		mEditTextReferee1 = (EditText) mView.findViewById(R.id.edt_referee1);
		mEditTextReferee2 = (EditText) mView.findViewById(R.id.edt_referee2);
		mEditTextGymnasium = (EditText) mView.findViewById(R.id.edt_gymnasium);
		mSpinnerNumberOfPeriod = (Spinner) mView
				.findViewById(R.id.spn_nb_period);
		mSpinnerTimePeriod = (Spinner) mView.findViewById(R.id.spn_time_period);
		mSpinnerShotTime = (Spinner) mView.findViewById(R.id.spn_shot_time);

		// mButtonNext.setOnClickListener(this);
		mRadioButtonNBA.setOnClickListener(this);
		mRadioButtonNCAA.setOnClickListener(this);
		mRadioButtonCustom.setOnClickListener(this);

		mRadioButtonNBA.setChecked(true);
		changeOptions(NBA_STANDAR_OPTION);

		return mView;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.radBtn_nba_standar) {
			changeOptions(NBA_STANDAR_OPTION);
			return;
		}
		if (v.getId() == R.id.radBtn_ncaa_standar) {
			changeOptions(NCAA_STANDAR_OPTION);
			return;
		}
		if (v.getId() == R.id.radBtn_custom_options) {
			changeOptions(CUSTOM_OPTION);
			return;
		}
	}

	private void changeOptions(int options) {
		switch (options) {
		case (NBA_STANDAR_OPTION): {
			ArrayAdapter<String> adapter_spn_nb_period = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					NB_PERIOD_NBA);
			ArrayAdapter<String> adapter_spn_time_period = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					TIME_PERIOD_NBA);
			ArrayAdapter<String> adapter_shot_time = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					SHOT_TIME_NBA);

			mSpinnerNumberOfPeriod.setAdapter(adapter_spn_nb_period);
			mSpinnerTimePeriod.setAdapter(adapter_spn_time_period);
			mSpinnerShotTime.setAdapter(adapter_shot_time);

			break;
		}
		case (NCAA_STANDAR_OPTION): {
			ArrayAdapter<String> adapter_spn_nb_period = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					NB_PERIOD_NCAA);
			ArrayAdapter<String> adapter_spn_time_period = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					TIME_PERIOD_NCAA);
			ArrayAdapter<String> adapter_shot_time = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					SHOT_TIME_NCAA);

			mSpinnerNumberOfPeriod.setAdapter(adapter_spn_nb_period);
			mSpinnerTimePeriod.setAdapter(adapter_spn_time_period);
			mSpinnerShotTime.setAdapter(adapter_shot_time);
			break;
		}
		case (CUSTOM_OPTION): {
			ArrayAdapter<String> adapter_spn_nb_period = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					NB_PERIOD_CT);
			ArrayAdapter<String> adapter_spn_time_period = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					TIME_PERIOD_CT);
			ArrayAdapter<String> adapter_shot_time = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_spinner_item,
					SHOT_TIME_CT);

			mSpinnerNumberOfPeriod.setAdapter(adapter_spn_nb_period);
			mSpinnerTimePeriod.setAdapter(adapter_spn_time_period);
			mSpinnerShotTime.setAdapter(adapter_shot_time);
			break;
		}
		}
	}

	public void fillData() {
		DataGlobal.matchGlobal.setTournament(mEditTextTournament.getText()
				.toString());
		DataGlobal.matchGlobal.setReferee1(mEditTextReferee1.getText()
				.toString());
		DataGlobal.matchGlobal.setReferee2(mEditTextReferee2.getText()
				.toString());
		DataGlobal.matchGlobal.setGymnasium(mEditTextGymnasium.getText()
				.toString());
		DataGlobal.matchGlobal.setDateTime(Calendar.getInstance().getTime()
				.toLocaleString());
		DataGlobal.matchGlobal.setShotTime(Integer.parseInt(mSpinnerShotTime
				.getSelectedItem() + ""));
		DataGlobal.matchGlobal.setTimePeriod(Integer
				.parseInt(mSpinnerTimePeriod.getSelectedItem() + ""));
		DataGlobal.matchGlobal.setNbPeriod(Integer
				.parseInt(mSpinnerNumberOfPeriod.getSelectedItem() + ""));

	}
}
